package com.chen.tliaswebtest.controller;

import com.chen.tliaswebtest.annotation.LogOperation;
import com.chen.tliaswebtest.pojo.Clazz;
import com.chen.tliaswebtest.pojo.PageResult;
import com.chen.tliaswebtest.pojo.Result;
import com.chen.tliaswebtest.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/7/24
 * 班级管理控制层
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    private final ClazzService clazzService;

    public ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    // 班级列表数据的条件分页查询
    @GetMapping
    public Result list(String name, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult result = clazzService.listByPage(name, begin, end, page, pageSize);
        return Result.success(result);
    }

    // 根据id删除班级
    @DeleteMapping("/{id}")
    @LogOperation
    public Result removeClazzById(@PathVariable Integer id) {
        clazzService.removeClazzById(id);
        return Result.success();
    }

    // 添加班级
    @PostMapping
    @LogOperation
    public Result addClazz(@RequestBody Clazz clazz) {
        clazzService.addClazz(clazz);
        return Result.success();
    }

    // 根据主键ID查询班级的信息
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id) {
        return Result.success(clazzService.getClazzById(id));
    }

    // 更新班级信息
    @PutMapping
    @LogOperation
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    // 查询所有班级信息
    @GetMapping("/list")
    public Result list() {
        return Result.success(clazzService.list());
    }
}
